package com.biz.primus.ms.oms.service.listener;

import com.biz.primus.model.oms.stream.OrderCollectionChannel;
import com.biz.primus.model.oms.stream.OrderStateChannel;
import com.biz.primus.model.oms.stream.OrderStateChannel.OrderStreamData;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import com.biz.primus.ms.oms.dao.repository.OmsOrderRepository;
import com.biz.primus.ms.oms.service.allocation.OrderAllocationService;
import com.biz.primus.ms.oms.service.collection.OmsCollectionService;
import com.biz.primus.ms.oms.service.returns.OrderReturnStateService;
import com.biz.primus.ms.oms.vo.AllocationRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

/**
 * @author yanweijin
 * @date 2017/11/17
 */
@Component
public class OrderStreamListener {


	@Autowired
	private OmsCollectionService omsCollectionService;

	@Autowired
	private OmsOrderRepository omsOrderRepository;

	@Autowired
	private OrderReturnStateService orderReturnStateService;

	@Autowired
	private OrderAllocationService allocationService;

//	@StreamListener(OrderStateSink.REFUND)
//	public void onOrderBegin(OrderStreamData data){
//		OmsOrder order = omsOrderRepository.findOne(data.getOrderId());
//		orderReturnStateService.newOmsRefund(null,order,"system");
//	}


	@StreamListener(OrderStateChannel.ORDER_BEGIN)
	public void onOrderBegin(OrderStreamData data){
		Long orderId = omsOrderRepository.findIdByOrderCode(data.getOrderCode());
		AllocationRequestVo req = AllocationRequestVo.builder().orderId(orderId).expectDepotCode(data.getExpectedDepotCode()).operator("system").build();
		allocationService.allocateOrder(req);
	}

	@StreamListener(OrderCollectionChannel.ORDER_DATA_INPUT)
	public void onNewOrder(OmsOrderVo order){
		omsCollectionService.collectionOrder(order);
	}






}
